package com.wtwd.campus.controller;

import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.common.RespCodeEntity;
import com.wtwd.campus.common.RespEntity;
import com.wtwd.campus.entity.AttendanceProduct;
import com.wtwd.campus.entity.AttendanceProductVO;
import com.wtwd.campus.entity.SysUser;
import com.wtwd.campus.model.vo.PageDataVO;
import com.wtwd.campus.service.AttendanceProductService;
import com.wtwd.campus.utils.AttendanceCommandUtils;
import com.wtwd.campus.utils.GetCurrentSysUserInfoUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author ldaoliang
 * @Date create in 9:41 2021/12/7
 * @Description 考勤机设备管理
 */
@RestController
@RequestMapping("/attendanceDevice")
public class AttendanceDeviceController {

    private static final Logger logger = LoggerFactory.getLogger(AttendanceDeviceController.class);

    @Autowired
    private AttendanceProductService attendanceProductService;
    @Autowired
    private GetCurrentSysUserInfoUtils getCurrentSysUserInfoUtils;
    @Autowired
    private AttendanceCommandUtils attendanceCommandUtils;

    /**
     * 考勤机设备列表
     *
     * @param schoolId  （只有选择学校之后才展示）
     * @param productNo 考勤机设备编号
     * @return
     */
    @GetMapping("/list")
    public RespEntity list(@RequestParam("schoolId") Integer schoolId, @RequestParam("productNo") String productNo,
                           @RequestParam("currPage") Integer currPage, @RequestParam("pageSize") Integer pageSize) {
        logger.info("getAttendanceList : schoolId = {}, productNo = {}", schoolId, productNo);
        // 只有选择学校之后才会展示，那么可以先查询出学校所有的考勤机，再从redis查询设备是否在线，根据productNo去比对在线与不在线
        PageDataVO<AttendanceProductVO> attendanceProductList = attendanceProductService.getAttendanceProductList(schoolId, productNo, currPage, pageSize);
        return new RespEntity(RespCode.SUCCESS, attendanceProductList);
    }

    /**
     * 添加考勤机设备
     *
     * @param attendanceProduct
     * @return
     */
    @PostMapping("/add")
    public RespCodeEntity add(@RequestBody AttendanceProduct attendanceProduct) {
        logger.info("addAttendanceProduct : param = {}", attendanceProduct);
        SysUser currentSysUserInfo = getCurrentSysUserInfoUtils.getCurrentSysUserInfo();
        return attendanceProductService.addAttendanceProduct(attendanceProduct, currentSysUserInfo.getUserId());
    }

    /**
     * 删除考勤机设备
     *
     * @param productId
     * @return
     */
    @DeleteMapping("/delete/{productId}")
    public RespCodeEntity delete(@PathVariable("productId") Integer productId) {
        logger.info("delAttendanceProduct : productId = " + productId);
        return attendanceProductService.deleteAttendanceProduct(productId);
    }

    /**
     * 更新考勤机信息
     *
     * @param attendanceProduct
     * @return
     */
    @PutMapping("/update")
    public RespCodeEntity update(@RequestBody AttendanceProduct attendanceProduct) {
        logger.info("updateAttendanceProduct : param = {}", attendanceProduct);
        SysUser currentSysUserInfo = getCurrentSysUserInfoUtils.getCurrentSysUserInfo();
        attendanceProduct.setUpdateUser(currentSysUserInfo.getUserId());
        attendanceProduct.setUpdateTime(System.currentTimeMillis());
        return attendanceProductService.updateAttendance(attendanceProduct);
    }

    /**
     * 时间校准
     *
     * @param productNo
     * @return
     */
    @GetMapping("/timeAdjust/{productNo}")
    public RespCodeEntity update(@PathVariable("productNo") String productNo) {
        SysUser currentSysUserInfo = getCurrentSysUserInfoUtils.getCurrentSysUserInfo();
        logger.info("timeAdjust : productNo = {} ,userId = {}", productNo,currentSysUserInfo.getUserId());
        boolean b = attendanceCommandUtils.sendTimeAdjusting(productNo);
        if (b){
            return new RespCodeEntity(RespCode.SUCCESS);
        }
        return new RespCodeEntity(RespCode.FAILED);
    }




}
